/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import structure.*;
import structure.Variable;

/**
 *
 * @author Gary Furlong
 */
public class StringParse {

    private static String currentString = "";
    public static DataFile data = new DataFile(0,0,0);

    public static void intoStruct(String input){
        currentString = input;

        String numberOfVariables = "";
        String numberRoots = "";
        String variableStates = "";
        String dataPoints = "";
        String dpToVars = "";

        numberOfVariables = split('\n',currentString);
        numberRoots = split('\n', currentString);
        variableStates = split('\n', currentString);
        dataPoints = split('\n', currentString);
        dpToVars = currentString;

        int intVars = Integer.parseInt(removeSpaces(numberOfVariables));
        int intRoots = Integer.parseInt(removeSpaces(numberRoots));
        int intDps = Integer.parseInt(removeSpaces(dataPoints));

        data = new DataFile(intVars,intRoots,intDps);

        currentString = variableStates;

        for(int i=1; i< intVars+1; i++){
            if(i < intRoots+1){
                data.addVariable(new Variable(i,Integer.parseInt(removeSpaces(split('\t',currentString))),true));
            }
            else{
                data.addVariable(new Variable(i,Integer.parseInt(removeSpaces(split('\t',currentString))),false));
            }
        }

        currentString = dpToVars;

        for(int i=1; i< intDps+1; i++){
            String currentDataPoint = split('\n', currentString);
            String nextCurrent = currentString;
            currentString = currentDataPoint;

            for(int j=1; j< intVars+1; j++){
                data.addDataPoint(new DataPoint(i,getVariableById(j),new State(getVariableById(j), Integer.parseInt(removeSpaces(split('\t',currentString))))));
            }

            currentString = nextCurrent;
        }
        
        //System.out.println("Data Points: " + data.getNumberOfDataPoints() +"\n");
        //System.out.println("Number of Variables: " + data.getNumberOfVariables()+"\n");
        //System.out.println("Number of Roots: " + data.getNumberOfRoots() +"\n");
        //System.out.println("Variable States:\n-----------------------------------------\n " + data.getVariables() + "\n");
        System.out.println("Data Points to Variables:\n-----------------------------------------\n" + data.getDataPoints());
    }

    private static String split(char splitChar, String splitString){
        String outString = "";
        char tempChar = splitString.charAt(0);
        int temp = -1;

        while (tempChar!=splitChar){
            ++temp;
            tempChar = splitString.charAt(temp);
            if (tempChar != splitChar){
            outString = outString + splitString.charAt(temp);
            }
            if(temp == splitString.length()-1){
                splitChar = tempChar;
            }
        }

        currentString = splitString.substring(temp+1);

        return outString;
    }

    private static String removeSpaces(String input){
        char currentChar;
        String out = "";
        for (int i=0; i<input.length(); i++){
            currentChar = input.charAt(i);
            if ((currentChar!='\t')&&(currentChar!='\r')){
                out = out + currentChar;
            }
        }
        return out;
    }

    public static Variable getVariableById(int id){
        for(Variable v : data.getVariables()){
            if (v.getId() == id){
                return v;
            }
        }
        throw new  IndexOutOfBoundsException("There is no variable with the inputted ID");
    }
}
